/* 
 * Copyright (c) 2022, CENTRIN.CIYUN.LTD. All rights reserved.
 */
package com.cory.admin.sys.controller.admin;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cory.admin.sys.controller.AdminController;
import com.cory.boot.domain.bean.Result;
import com.cory.boot.domain.enumdef.EOperateType;
import com.cory.boot.sys.entity.SysParam;
import com.cory.boot.sys.service.SysParamService;
import com.cory.utils.lang.StringUtils;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;

/**
 * @author xiongcong
 * @date 2022-01-13
 */
@Controller
@RequestMapping("admin/sys/param")
public class SysParamController extends AdminController {

    @Autowired
    private SysParamService sysParamService;
    
    @RequestMapping("list")
    public String list(SysParam param, Model model) {
        param.setSysType(2);
        super.startPage();
        List<SysParam> list = sysParamService.list(param);
        
        model.addAttribute("page", new PageInfo<>(list));
        model.addAttribute("param", param);
        return "admin/sys/param/list";
    }
    
    @RequestMapping("form")
    public String form(String paramName, Model model) {
        if (StringUtils.isNotBlank(paramName)) {
            model.addAttribute("info", sysParamService.getById(paramName));
            model.addAttribute("operate", EOperateType.UPDATE.key);
        } else {
            model.addAttribute("operate", EOperateType.ADD.key);
        }
        return "admin/sys/param/form";
    }
    
    @RequestMapping("save")
    @ResponseBody
    public Result<?> save(SysParam info, Integer operate) {
        
        if (operate == EOperateType.ADD.key) {
            if (sysParamService.getById(info.getParamName()) != null) {
                return Result.fail("参数已存在");
            }
            
            info.setSysType(2);
            sysParamService.save(info);
        } else {
            sysParamService.updateById(info);
        }
        
        return Result.success("保存成功");
    }
    
    @RequestMapping("delete")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result<?> delete(String id, String ids) {
        List<String> idList = id != null ? Lists.newArrayList(id) : 
            Arrays.asList(ids.split(",")).stream().filter(item -> StringUtils.isNotBlank(item))
            .collect(Collectors.toList());
        
        if (idList.size() > 0) {
            for (String i : idList) {
                sysParamService.deleteById(i);
            }
        }
        
        return Result.success((idList.size() > 1 ? "批量" : "") + "删除成功");
    }
}
